package com.bitauto.i.commonUtils.leet;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 在数组中找到指定结果的两个数
 */
public class Answer1 {
    public static void main(String[] args) {
        int[] nums={1,2,3,4,5,3};
        int target=6;
        int[] result1 = twoSum1(nums,target);
        System.out.println(result1);
        int[] result2 = twoSum2(nums,target);
        System.out.println(result2);
    }

    public static int[] twoSum1(int[] nums, int target) {
        int[] result = new int[2];
        for (int i=0;i<nums.length-1;i++) {
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    result[0]=i;
                    result[1]=j;
                }
            }
        }
        return result;
    }

    public static int[] twoSum2(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for (int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])){
                result[0]= map.get(target-nums[i]);
                result[1]=i;
            }else{
                map.put(nums[i],i);
            }
        }
        return result;
    }
}
